<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css"/>
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css"/>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/common.js"></script>

    <script>

        // 页面加载完毕
        $(function () {
            // 获取地址栏上的id
            var strategyId = getParams().id;
            // 发送异步请求查询指定 id 大攻略的所有攻略分类
            $.get("/strategies/" + strategyId, function (data) {
                $(".title").html(data.title);
            });

            // 发送异步请求获取大攻略下的所有分类攻略
            $.get("/strategies/" + strategyId + "/catalogs", function (data) {
                var num = 0;
                $(".catalogs").renderValues({list: data}, {
                    showTitle: function (ele, value) {
                        var content = ++num + value;
                        if (num < 10) {
                            content = "0" + content;
                        }
                        $(ele).html(content);
                    },
                    handle: function (ele, value) {
                        ele.href = "/strategyDetails.html?id=" + value;
                    }
                })
            });

            //查询大攻略下所有的游记
            $.get("/strategies/"+strategyId+"/travels",function (data) {
                $(".travels").renderValues({list:data},{
                    handle:function (ele,value) {
                        ele.href = "/travelContent.html?id="+value;
                    }
                })
            })

            // 获取用户信息
            var user = JSON.parse(sessionStorage.getItem("user"));

            if (strategyId) {
                // 发送ajax请求获取游记点赞信息
                $.get("/strategies/" + strategyId + "/greats", {greatType: 'strategy_id'}, function (data) {

                    // 渲染点赞数据
                    $("#likeBtn").html("&nbsp" + data.greats);

                });

                // 给点赞按钮绑定点击事件
                $(".focus .fa-thumbs-up").click(function () {
                    if (user) {
                        // 用户登录过, 发送ajax请求到后台进行点赞 或 取消点赞的处理
                        $.ajax({
                            url: "/strategies/" + strategyId + "/greats",
                            type: "post",
                            data: {greatType: 'strategy_id', userId: user.id},
                            success: function (data) {
                                // 渲染点赞数据
                                $("#likeBtn").html("&nbsp" + data.greats);
                            }
                        });

                    } else {
                        // 跳转到登录页面进行登录操作
                        $(document).dialog({
                            type: "confirm",
                            titleShow: false,
                            content: '您尚未登录',
                            buttons: [
                                {
                                    name: '跳转到登录页面',
                                    callback: function () {
                                        location.href = "/login.html";
                                    }
                                }
                            ]
                        });
                    }
                });

                //发送ajax请求,获取某篇攻略的点评
                var currentPage = 1;//当前页
                var pages;//总页数
                var comments;//点评数组
                $.get("/strategies/" + strategyId + "/strategyComments", {pageSize: 2, state: 2}, function (data) {
                    //渲染页面
                    $("#comment").renderValues(data, {
                        dispose: function (ele, value) {
                            for (var i = 0; i < value; i++) {
                                $(ele).append($("<i class='fa fa-star'></i>"));
                            }
                            for (var j = value; j < 5; j++) {
                                $(ele).append($("<i class='fa fa-star-o'></i>"));
                            }
                        },
                        manage:function (ele, value) {
                            ele.href = "/strategyComment.html?id="+value;
                        },
                        conduct:function (ele, value) {
                            ele.href = "/strategyComment.html?id="+value;
                        }, gethref: function (ele, value) {
                            ele.href = "/userProfiles.html?authorId=" + value;
                        },
                        getReply:function (ele, value) {
                            //将异步请求变成同步请求,可以回显点赞数
                            $.ajax({
                                url: "/strategyComments/"+value+"/replyCount",
                                type: "get",
                                async: false,
                                success: function (data) {
                                    $(ele).html("评论("+data.replyCount+")");
                                }
                            })
                        }
                    });
                    pages = data.pages;
                    comments = data.list;
                })

                //点评分页
                $(window).scroll(function () {
                    // 判断是否触底
                    if (isEnd()) {
                        // 判断是否还有下一页
                        if (currentPage < pages) {
                            // 当前页+1
                            currentPage++;
                            // 发送ajax请求
                            var args = {state: 2, currentPage: currentPage, pageSize: 2};
                            $.get("/strategies/" + strategyId + "/strategyComments", args, function (data) {
                                // 拼接游记数组数据
                                $.merge(comments, data.list);
                                // 渲染页面
                                $("#comment").renderValues({list: comments}, {
                                    dispose: function (ele, value) {
                                        for (var i = 0; i < value; i++) {
                                            $(ele).append($("<i class='fa fa-star'></i>"));
                                        }
                                        for (var j = value; j < 5; j++) {
                                            $(ele).append($("<i class='fa fa-star-o'></i>"));
                                        }
                                    },
                                    manage:function (ele, value) {
                                        ele.href = "/strategyComment.html?id="+value;
                                    },
                                    conduct:function (ele, value) {
                                        ele.href = "/strategyComment.html?id="+value;
                                    },
                                    gethref: function (ele, value) {
                                        ele.href = "/userProfiles.html?authorId=" + value;
                                    },
                                    getReply:function (ele, value) {
                                    //将异步请求变成同步请求,可以回显点赞数
                                    $.ajax({
                                        url: "/strategyComments/"+value+"/replyCount",
                                        type: "get",
                                        async: false,
                                        success: function (data) {
                                            $(ele).html("评论("+data.replyCount+")");
                                        }
                                    })
                                }
                                });
                            });
                        } else {
                            // 否则, 提示已到低
                            $(document).dialog({
                                type: 'notice',
                                infoText: '已到底部',
                                autoClose: 1500,
                                position: 'bottom'  // center: 居中; bottom: 底部
                            });
                        }
                    }
                });

                // 定义一个判断屏幕溢出高度 + 屏幕高度 > 网页高度的函数
                function isEnd() {
                    return $(window).height() + $(window).scrollTop() >= $(document).height() - 1;
                }

                // 渲染页面的方法
                function renderViews(data) {
                    $(".travels").renderValues(data, {
                        // 动态渲染a标签
                        handle: function (ele, value) {
                            $(ele).attr("href", "/travelContent.html?id=" + value);
                        }
                    });
                }


                //点击点评跳转到点评界面
                $("#commentBtn").click(function () {
                    if (user) {
                        document.location.href = "/mine/addComment.html?strategyId=" + strategyId;
                    } else {
                        // 跳转到登录页面进行登录操作
                        $(document).dialog({
                            type: "confirm",
                            titleShow: false,
                            content: '您尚未登录',
                            buttons: [
                                {
                                    name: '跳转到登录页面',
                                    callback: function () {
                                        location.href = "/login.html";
                                    }
                                }
                            ]
                        });
                    }
                })

                //渲染标签
                $.get('/strategies/'+strategyId+'/tags',function (data) {
                    $(".tag").renderValues({list:data})
                })


                //进入页面发送请求查询收藏数量
                $.get("/collect/count",{collectId:strategyId,type:2},function (data) {
                    var collectNum = 0;
                    if (data){
                        collectNum = data;
                    }
                    //渲染数量
                    $("#collectNum").html(collectNum);
                    //给收藏按钮添加空爱心
                    $("#collect").addClass("fa-heart-o");
                })


                //判断是否登录
                if (user) {
                    //发送请求查询收藏内容
                    $.get("/collect/one", {userId: user.id, collectId: strategyId, type: 2}, function (data) {
                        if (data) {
                            //清除空爱心
                            $("#collect").removeClass("fa-heart-o");
                            //添加实心爱心
                            $("#collect").addClass("fa-heart");
                        } else {
                            //
                            $("#collect").addClass("fa-heart-o");
                        }

                    })
                }
                    //给收藏按钮添加绑定事件
                    $("#collect").click(function () {
                        if (user){
                            $.ajax({
                                url:"/collect",
                                type:"post",
                                data:{userId:user.id,collectId:strategyId,type:2},
                                success:function (data) {
                                    //如果没有则没有颜色
                                    if (data){
                                        $("#collect").removeClass("fa-heart");
                                        $("#collect").addClass("fa-heart-o");
                                    }else {
                                        $("#collect").removeClass("fa-heart-o");
                                        $("#collect").addClass("fa-heart");
                                    }
                                    //再次查询数量
                                    $.get("/collect/count",{collectId:strategyId,type:2},function (data) {
                                        var collectNum = 0;
                                        if (data){
                                            collectNum = data;
                                        }
                                        //渲染数量
                                        $("#collectNum").html(collectNum);
                                    })
                                }
                            });
                        }else {
                            // 跳转到登录页面进行登录操作
                            $(document).dialog({
                                type: "confirm",
                                titleShow: false,
                                content: '您尚未登录',
                                buttons: [
                                    {
                                        name: '跳转到登录页面',
                                        callback: function () {
                                            location.href = "/login.html";
                                        }
                                    }
                                ]
                            });
                        }

                    })


            }

        });

    </script>


</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title"></h5>
    <small><span>0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa" id="collect"><span id="collectNum"> 0</span> 收藏</i>
        <i class="fa fa-thumbs-up"><span id="likeBtn"></span> 点赞</i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container travels" >
            <h6>大家都在看</h6>
            <div render-loop="list">
                <a href="travelContent.html" render-key="list.id" render-fun="handle">
                    <button class="btn" render-html="list.title"></button>
                </a>
            </div>
        </div>
        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="showTitle"></h6>
                    <div render-loop="list.details">
                        <a href="strategyDetails.html" render-key="list.details.id" render-fun="handle">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div render-loop="list">
                <span render-html="list.tag">价格便宜</span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a href="/userProfiles.html?authorId=" render-key="list.user.id" render-fun="gethref">
                            <img class="rounded-circle"
                                 render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName">
                            <a href="/userProfiles.html?authorId=" render-key="list.user.id" render-fun="gethref">
                                <span render-html="list.user.nickName"></span></a></p>
                        <span class="comment-star" render-key="list.star" render-fun="dispose">
                            <!--<i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star-o"></i>-->
                        </span>

                        <span class="comment-date" render-html="list.releaseTime"></span>
                        <div class="comment-content">
                            <a render-key="list.id" render-fun="conduct">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.commentImages">
                            <li>
                                <img render-src="list.commentImages.image">
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <a render-key="list.id" render-fun="manage">
                                <i class="fa  fa-commenting-o " style="margin-right: 10px" render-key="list.id" render-fun="getReply"> 评论 </i>
                                <i class="fa fa-thumbs-o-up"> <span render-html="list.greatCount"></span> </i>
                            </a>
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>

</body>
</html>